package com.stud.system.controller.tool;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stud.common.core.domain.Result;
import com.stud.system.domain.QueryConfig;
import com.stud.system.service.QueryConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.stud.common.annotation.Log;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.enums.BusinessType;
import com.stud.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import com.stud.common.core.page.TableDataInfo;

/**
 * 简单通查Controller
 *
 * @author Stud
 * @date 2022-11-11
 */
@Api(tags = "简单通查")
@RestController
@RequestMapping("/tool/queryConfig")
public class QueryConfigController extends BaseController {

    @Autowired
    private QueryConfigService queryConfigService;

    @ApiOperation("查询简单通查列表")
    @PreAuthorize("@ss.hasPermi('tool:query-config:list')")
    @GetMapping("/list")
    public TableDataInfo<QueryConfig> list(QueryConfig queryConfig) {
        startPage();
        Wrapper<QueryConfig> wrapper = new QueryWrapper(queryConfig);
        List<QueryConfig> list = queryConfigService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation("导出简单通查列表")
    @PreAuthorize("@ss.hasPermi('tool:query-config:export')")
    @Log(title = "简单通查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryConfig queryConfig) {
        Wrapper<QueryConfig> wrapper = new QueryWrapper(queryConfig);
        List<QueryConfig> list = queryConfigService.list(wrapper);
        ExcelUtil<QueryConfig> util = new ExcelUtil<QueryConfig>(QueryConfig.class);
        util.exportExcel(response, list, "简单通查数据");
    }

    @ApiOperation("获取简单通查详细信息")
    @PreAuthorize("@ss.hasPermi('tool:query-config:query')")
    @GetMapping(value = "/{id}")
    public Result<QueryConfig> getInfo(@PathVariable("id") Long id) {
        return Result.ok(queryConfigService.getById(id));
    }

    @ApiOperation("新增简单通查")
    @PreAuthorize("@ss.hasPermi('tool:query-config:add')")
    @Log(title = "简单通查", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody QueryConfig queryConfig) {
        return Result.ok(queryConfigService.save(queryConfig));
    }

    @ApiOperation("修改简单通查")
    @PreAuthorize("@ss.hasPermi('tool:query-config:edit')")
    @Log(title = "简单通查", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody QueryConfig queryConfig) {
        return Result.ok(queryConfigService.update(queryConfig));
    }

    @ApiOperation("删除简单通查")
    @PreAuthorize("@ss.hasPermi('tool:query-config:remove')")
    @Log(title = "简单通查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(queryConfigService.removeByIds(ids));
    }


    @PostMapping("/generalSearch/{queryCode}")
    @ApiOperation(value = "run sql")
    public Result runSQL(@PathVariable String queryCode, HttpServletRequest request) {
        return queryConfigService.queryConfigByCodeRunSQL(queryCode, request);
    }


}
